from PySide6.QtCore import QDateTime, Slot, Qt
from PySide6.QtWidgets import QMessageBox, QDialog, QTableWidgetItem, QMainWindow, QButtonGroup
from PySide6.QtGui import QIcon, QColor

from Devices.Chiot.BsDevs.ComDev.Forms.ComDevWindow import ComDevWindow
from Devices.Chiot.BsDevs.FallSensor.FallSensorDevice import FallSensorDevice
from Devices.Chiot.BsDevs.FallSensor.FallSensorParser import FallSensorParser
from Devices.Chiot.BsDevs.FallSensor.Forms.FallSensorWindow_UI import Ui_FormFallSensorWindow
from Components.MessageBoxFunc import MessageBoxFunc

class FallSensorWindow(ComDevWindow):
    # 0、窗体初始化、状态变量初始化
    def __init__(self, supportDevice, parent = None):
        # 注意在实现特有设备时，一定要先将其ui对象加载完毕
        super().__init__(supportDevice, parent)

        self.FullStatus = FallSensorDevice.FallSensorFullStatus()

        self._ui = Ui_FormFallSensorWindow()
        self._ui.setupUi(self)
        self.initUiWindow()
        # self.testUi()
        self.SetupUi(self._ui)

    # 1、数据解析
    def setHeartbeatStatus(self):
        pass

    def CmdRptStatusReceived(self, msgValue):
        if FallSensorParser.ParseHeartbeat(msgValue, self.FullStatus):
            self.setFullStatus()

    def CmdHeartbeatReceived(self, msgValue):
        if FallSensorParser.ParseHeartbeat(msgValue, self.FullStatus):
            self.setFullStatus()

    def CmdFullStatusReceived(self, msgValue):
        # 雷达信息
        if FallSensorParser.ParseHeartbeat(msgValue, self.FullStatus):
            self._ui.label_radarModel.setText(self.FullStatus.radarModel)
            self._ui.label_radarVer.setText(self.FullStatus.radarVer)
            self._ui.spinBox_installHeight.setValue(int(self.FullStatus.fixHeight))

    def CmdMsgValueReceived(self, msgName, msgValue):
        pass

    # 2、运行状态
    def setFullStatus(self):
        self._ui.label_existStatus.setText("有人" if self.FullStatus.existStatus else "无人")
        if self.FullStatus.motionStatus == FallSensorDevice.MotionStatus.NO_MOVE:
            self._ui.label_moveStatus.setText("无人")
        elif self.FullStatus.motionStatus == FallSensorDevice.MotionStatus.STOP_MOVE:
            self._ui.label_moveStatus.setText("静止")
        elif self.FullStatus.motionStatus == FallSensorDevice.MotionStatus.MOVING:
            self._ui.label_moveStatus.setText("移动中")
        self._ui.label_fallStatus.setText("发生" if self.FullStatus.fallStatus else "未发生")
        self._ui.label_staticStatus.setText("发生" if self.FullStatus.staticStatus else "未发生")
        self._ui.label_InstallHeight.setText(str(self.FullStatus.fixHeight))
        if 0 != self.FullStatus.fallTime:
            self._ui.label_fallTime.setText(QDateTime.fromSecsSinceEpoch(self.FullStatus.fallTime).toString("yyyy-MM-dd HH:mm:ss"))
        if 0 != self.FullStatus.staticTime:
            self._ui.label_staticTime.setText(QDateTime.fromSecsSinceEpoch(self.FullStatus.staticTime).toString("yyyy-MM-dd HH:mm:ss"))

    # 2、雷达信息页
    @Slot()
    def on_pushButton_setInstallHeight_clicked(self):
        cmd = FallSensorParser.PackSetAttr("fixHeight", "height", int(self._ui.spinBox_installHeight.value()))
        print(cmd)
        self.SendCommandText(cmd)
                              
